import routes from './routes';
import { getToken } from '@/utils/token-util';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';

//白名单
const whiteList = ['/login', '/about', '/help'];

//路由守卫
routes.beforeEach((to, from, next) => {

    if (to.meta && to.meta.title) {
        document.title = `${to.meta.title} | 智慧灯杆系统`
    } else {
        document.title = '智慧灯杆系统';
    }

    const hasToken = getToken();
    if (hasToken) {
        NProgress.start();
        next();
    } else if (whiteList.some(m => to.path.startsWith(m))) {
        // 确保一定要调用 next()
        next();
    } else {
        next({
            path: '/login'
        });
    }
})

/*全局后置钩子*/
routes.afterEach(() => {
    // finish progress bar
    NProgress.done()
})

